#include <stdio.h>
#include <stdlib.h>
#include "dominion.h"
#include "dominion_helpers.h"
#include "rngs.h"

//This Unit Test Will Test the Outpost Card.

//initialize game struct to array of 0's
void clearGameState(struct gameState *G) {
	int i;
	for (i=0; i<sizeof(struct gameState); i++) {
		((char *)G)[i] = 0;
	}
}

int main() {
	int i, j;
	int currentPlayer, handCount;	//relevant to refactored outpost in dominion.c
	int seed = 1003;	//FEEL FREE TO CHANGE
	int num_tests = 100;	//FEEL FREE TO CHANGE
	int played_errors, handCount_errors = 0;			//number of error'd tests for each type of error in outpost
	//int turn_errors;
	
	//Initialize Two Game States that will be compared.
	struct gameState G, G2;
	
	//initialize random number using rngs.c library
	SelectStream(1);
	PutSeed((long)seed);
	
	printf("Now testing cardtest2 (Outpost card)\n");
	
	for (i=0; i<num_tests; i++) {
	
		//initialize blank game state
		clearGameState(&G);
		clearGameState(&G2);
		
		//get random player number 2-4, and assign them to the appropriate turn in the game state.
		currentPlayer = (int)(Random()*2)+2;		//check this for whoseTurn
		G.whoseTurn = G2.whoseTurn = currentPlayer;
		
		//get random hand size and assign in both game states
		handCount = (int)(Random()*MAX_HAND);
		G.handCount[currentPlayer] = G2.handCount[currentPlayer] = handCount;
		
		 //get random cards to fill these handcounts and assign in the game states
		for (j=0; j<handCount; j++) {
			G.hand[currentPlayer][j] = G2.hand[currentPlayer][j] = (int)(Random()*MAX_HAND); 
		}
		
		//put outpost card in each player's hand.
		G.handCount[currentPlayer]++;
		G2.handCount[currentPlayer]++;
		G.hand[currentPlayer][handCount] = G2.hand[currentPlayer][handCount] = outpost;
		
		//Call outpost function on ONLY GameState 1.
		cardEffect(outpost, 0, 0, 0, &G, handCount, NULL);
		
		//Now that G has had the Outpost card played, test ALL things done by the Outpost Card by comparing G and G2
		//Test that outpost-played flag was incremented properly.
		if (G.outpostPlayed != ((G2.outpostPlayed)+1) ) {
			printf("Error: Outpost played was not incremented properly\n");
			played_errors++;
		}
		
		//Test that the handcount is correct after discarding
		if (G.handCount[currentPlayer] != (G2.handCount[currentPlayer]-1)) {
			printf("Error: Hand size has not been decremented by one\n");
			handCount_errors++;
		}
		
		//WHERE IS THE TURN INCREMENT? ADD TO BUG LIST! Isn't in end_turn either!
		
	} //end main for loop
	
	printf("Out of %d tests, there were %d outpostPlayed errors, %d handCount errors\n\n", num_tests, played_errors, handCount_errors);
	
	return 0;
}

//For reference
/*
case outpost:
      //set outpost flag
      state->outpostPlayed++;
			
      //discard card
      discardCard(handPos, currentPlayer, state, 0);
      return 0;
*/